/**
 * @version 3 11 February 2013
 * @author Myhaylo Kotsyuruba
 */

public class ManyCorrectAnswersQuestions extends ManyAnswersQuestions{
    
    private static final int CODE_SELECTED_ANSWER = 1;
    
    public ManyCorrectAnswersQuestions(String text, int score, 
            Answer[] answers) {
        super(text, score, answers);
    }

    @Override
    public AnswerMap convertsUserAnswer(String userAnswer) {
        String[] splitedUserAnswers = userAnswer.split(SPLASH);
        AnswerMap userAnswerMap = new AnswerMap(getQuantityAnswers());
        for (String partOfUserAnswer : splitedUserAnswers) {
            int indexUserAnswer = Integer.parseInt(partOfUserAnswer) - 1;
            userAnswerMap.setField(indexUserAnswer, CODE_SELECTED_ANSWER);
        }
        return userAnswerMap;
    }
    
    @Override
    public AnswerMap getCorrectAnswerMap() {
        AnswerMap correctAnswerMap = new AnswerMap(getQuantityAnswers());
        for (int index = 0; index < getQuantityAnswers(); index++) {
            if (isCorrectAnswer(index)) {
                correctAnswerMap.setField(index, CODE_SELECTED_ANSWER);
            }
        }
        return correctAnswerMap;
    }

    @Override
    public Question clone() {
        return new ManyCorrectAnswersQuestions(getText(), getScore(),
                getAnswers());
    }

    @Override
    public String toString() {
        String result = String.format("%s%nPlease enter the numbers of correct"
                + " responses, splited by spaces.", super.toString());
        return result;
    }
}
